<?php

namespace App\Service\delivery;
use App\Model\SystemOrderDay;
use App\Service\CommicService;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
use Hyperf\DbConnection\Db;
class DashboardService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
        $this->userAppletIds = $this->getUserAppletInfo();
    }

    public function search(array $params)
    {
        $applet_ids = $params['applet_ids'] ?? '';
        $delivery_user_id = $params['delivery_user_id'] ?? '';
        if (empty($applet_ids)) {
            return [];
        }
        $subSelect = '';
        //2023-12-11 新增可看到投放组员的数据
        if (!empty($delivery_user_id)) {
            $delivery_str = implode(',', $delivery_user_id);
            $subSelect = "select `id` from `data_spread` where `user_id` in (" . $delivery_str . ") and `data_spread`.`deleted_at` is null";
        } else {
            $userInfo = $this->getUserAppletInfo(2);
            //管理员能看到所有
            if ($userInfo['role']['is_admin'] != 1) {
                $subSelect = "select `id` from `data_spread` where `user_id` = " . $userInfo['id'] . " and `data_spread`.`deleted_at` is null";
            }
        }
        //获取所有的小程序所绑定的短剧
        if ($applet_ids == '1') {
            $applet_id = $this->userAppletIds;
        } else {
            $applet_id = [$applet_ids];
        }
        $selectArr = ['date'];
        $groupBy = ['date'];
        $countArr = [
            Db::raw('SUM(order_count) as order_count'), //支付笔数
            Db::raw('SUM(first_user_count) as first_user_count'), //新用户充值人数
            Db::raw('SUM(first_user_pay_amonut) as first_user_pay_amonut'),
            Db::raw('SUM(today_first_user_pay_amonut) as today_first_user_pay_amonut'),//新用户首次充值金额
            Db::raw('SUM(today_first_user_pay_count) as today_first_user_pay_count'), //新用户首次充值笔数
            Db::raw('SUM(today_after_user_pay_amonut) as today_after_user_pay_amonut'),//新用户充值金额
            Db::raw('SUM(today_after_user_pay_count) as today_after_user_pay_count'),//新用户复充值笔数
            Db::raw('SUM(order_amount) as order_amount'), //总充值金额
        ];

        $selectArr = array_merge($selectArr, $countArr);
        //获取所拥有的
        $query = SystemOrderDay::query()->select($selectArr);

        if ($applet_id != '' && $applet_ids != 1) {
            $query->where('system_order_day.applet_id', $applet_ids);
        }
        if (!empty($subSelect)) {
            $query->whereRaw('data_spread_id in (' . $subSelect . ')');
        }

//        $date[0] = date('Y-m-d',time());
//        $date[1] = date('Y-m-d',time()-86400);
//
//        $query->whereBetween('date',$date);
        $query->groupBy($groupBy);

        $query->where('data_spread_id', '!=', 0);
        $models = $query->orderBy('date','desc')->get()->toArray();
        $returnData = [];
        foreach ($models as $item) {
            $data = array();

            $data['date'] = $item['date'];
            //新用户充值人数
            $data['new_user_count'] = $item['first_user_count'];
            //新用户充值笔数
            $data['new_user_pay_count'] = $item['today_first_user_pay_count']+$item['today_after_user_pay_count'];
            //总充值金额
            $data['order_amount'] = sprintf("%.2f", floatval($item['order_amount'] / 100));
            //支付笔数
            $data['order_count'] = $item['order_count'];
            //新用户充值金额
            $data['new_user_pay_amonut'] = sprintf("%.2f", floatval(($item['today_first_user_pay_amonut'] + $item['today_after_user_pay_amonut'])/100));
            //新用户人均充值
            if(!empty($data['new_user_pay_amonut']) && !empty($data['new_user_count'])){
                $data['new_user_average'] = $data['new_user_pay_amonut']/$data['new_user_count'];
            }else{
                $data['new_user_average'] = 0;
            }
            //副充率
            if(!empty($data['today_after_user_pay_count']) && !empty($data['order_count'])){
                $data['after_rate'] = $data['today_after_user_pay_count']/$data['order_count'];
            }else{
                $data['after_rate'] = 0;
            }
            //首充率
            if(!empty($data['today_first_user_pay_count']) && !empty($data['order_count'])){
                $data['first_rate'] = $item['today_first_user_pay_count']/$data['order_count'];
            }else{
                $data['first_rate'] = 0;
            }
            //用户付费up值
            if (!empty($item['today_first_user_pay_count']) && !empty($data['new_user_pay_amonut'])) {
                $data['today_user_arpu'] = sprintf("%.2f", ($data['new_user_pay_amonut'] / $item['today_first_user_pay_count']));
            } else {
                $data['today_user_arpu'] = 0;
            }
            $returnData[] = $data;
        }
        return $returnData;
    }
}